//
//  StatusHomeCell.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/5/11.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import KILabel

 /// 枚举
enum HomeTableCellIdntifier: String{
    case OriginalCell = "OriginalCell"
    case ForwardCell = "ForwardCell"
    
    // 在枚举中用static修饰一个方法，相当于类中用class修饰的方法
    static  func returnCellIdntifier(status: Status) ->String{
        return status.retweeted_status != nil ? ForwardCell.rawValue : OriginalCell.rawValue
    }
}




let padding: CGFloat = 10
class StatusHomeCell: UITableViewCell {
     /// 插图的size
    var colltionViewSize: CGSize = CGSizeZero
     /// 插图上边距
    var  colltionTop: CGFloat = 0
    
    var  status: Status?{
        didSet{
            // 给topview赋值
            topVIew.status = status
            
            //给正文赋值
          //  contentLabel.text = status?.text
            contentLabel.attributedText = EmoticonPackage.returnAttritbute(status!.text!, font: contentLabel.font)
            let textSize =  contentLabel.sizeThatFits(CGSize(width:UIScreen.mainScreen().bounds.size.width-20 , height: CGFloat(MAXFLOAT)))
                
                //contentLabel.adaptLabLineSpacing(3, maxSize:CGSize(width:UIScreen.mainScreen().bounds.size.width-20 , height: CGFloat(MAXFLOAT)) , text: contentLabel.text!)
            //update constraint
            contentLabel.snp_updateConstraints { (make) in
            //  make.width.equalTo(textSize.width)
                make.height.equalTo(textSize.height)
            }
            
             colltionView.status = status
             let sizee = colltionView.calculateImageSize()
            colltionViewSize = sizee.viewSize
            colltionTop = status?.withMap_urls?.count != 0 ? 10 : 0
             //update constraint
//           colltionView.snp_updateConstraints { (make) in
//           make.width.equalTo(sizee.viewSize.width)
//           make.height.equalTo(sizee.viewSize.height)
//            
//            }
             colltionView.reloadData()
        }
    }
    
    // 自定义的类需要重写init 方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 获取cell的高度
    func getCellHeight(sts: Status) ->CGFloat{
        self.status = sts
        self.layoutIfNeeded()
        return CGRectGetMaxY(footView.frame)
    }
    

      func setUpUI() {
       contentView.addSubview(topVIew)
        contentView.addSubview(contentLabel)
        contentView.addSubview(footView)
        contentView.addSubview(colltionView)
        // 设置背景色证明， 背景图片下面有一部分，透明度
        footView.backgroundColor = UIColor(white: 0.2, alpha: 0.5)

        topVIew.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(0)
            make.height.equalTo(60)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(topVIew.snp_bottom).offset(10)
            make.centerX.equalTo(self)
            make.width.equalTo(UIScreen.mainScreen().bounds.size.width-20)
            make.height.equalTo(1)
            // make.bottom.equalTo(colltionView.snp_top).offset(-10)
        }

        
        colltionView.snp_makeConstraints { (make) in
//           make.left.equalTo(contentLabel)
//           make.top.equalTo(contentLabel.snp_bottom).offset(10)
           make.width.equalTo(1)
            make.height.equalTo(1)
            // make.bottom.equalTo(footView.snp_top).offset(-10)
      }
        
        // 自动适配高度的时候， 底部要和
//        footView.snp_makeConstraints { (make) in
//            make.left.right.equalTo(contentView)
//            make.height.equalTo(44)
//            make.bottom.equalTo(contentView).offset(-10)
//        }
        
        footView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.height.equalTo(44)
            make.top.equalTo(colltionView.snp_bottom).offset(10)
        }
    }
    

     /// 顶部的view
    private lazy var topVIew: CellTopView = CellTopView(frame: CGRectZero)
    
    /// 正文
     lazy var contentLabel: KILabel =
        {
            let label = KILabel()
            label.textColor = UIColor.darkGrayColor()
            label.numberOfLines = 0
            label.font = UIFont.systemFontOfSize(15)
            label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 20
            return label
    }()

    
    /// 插图 colltionView
     lazy var colltionView: CellPictureBrowserView = CellPictureBrowserView()

     /// 底部工具条
     lazy var footView: CellBottomView =  CellBottomView()
}








